package stark.app.function.pixeldraw;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.pixeldraw.lib.db.PdDbUtil;
import com.stark.pixeldraw.lib.db.PdRecord;
import com.stark.pixeldraw.lib.ui.PixelDrawEditActivity;

import java.util.List;

import stark.app.R;
import stark.app.databinding.FragmentPixelDrawRecordBinding;
import stark.common.basic.base.BaseNoModelFragment;

/**
 * Author：daizhongbin on 2024/4/12
 * Email：1027154338@qq.com
 * Desc:
 */
public class PixelDrawRecordFragment extends BaseNoModelFragment<FragmentPixelDrawRecordBinding> {

    private PixelDrawRecordAdapter mRecordAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_pixel_draw_record;
    }

    @Override
    protected void initView() {
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
        mDataBinding.rvRecord.setLayoutManager(layoutManager);
        PixelDrawRecordAdapter recordAdapter = new PixelDrawRecordAdapter();
        mRecordAdapter = recordAdapter;
        recordAdapter.setOnItemClickListener(this);
        mDataBinding.rvRecord.setAdapter(recordAdapter);
    }

    @Override
    protected void initData() {
        PdDbUtil.getPdRecordsForLiveData(0, PdDbUtil.getPdRecordsCount()).observe(this, new Observer<List<PdRecord>>() {
            @Override
            public void onChanged(List<PdRecord> pdRecords) {
                mRecordAdapter.setNewInstance(pdRecords);
                if (mRecordAdapter.getItemCount() == 0) {
                    mDataBinding.tvNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        PdRecord record = mRecordAdapter.getItem(position);
        PixelDrawEditActivity.start(getActivity(), record);
    }

}
